package com.shopcart.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.dialet.PaginationSupport;
import com.demo.util.SqlUtils;
import com.shopcart.daoEx.model.StoreCategoryEx;
import com.shopcart.domain.StoreCategory;
import com.shopcart.service.StoreCategoryService;
import com.vsd.repository.StoreCategoryRepository;

@Service
@Transactional
public class StoreCategoryServiceImpl implements StoreCategoryService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(StoreCategoryServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private StoreCategoryRepository storeCategoryRepository;

	@Override
	public Collection<StoreCategory> findByLevel(Integer level, Sort sort) {
		return storeCategoryRepository.findByLevel(level, sort);
	}

	@Override
	public Page<StoreCategory> findAll(Pageable p) {
		return storeCategoryRepository.findAll(p);
	}
	
	@Override
	public StoreCategory findByCode(String code){
		Collection<StoreCategory> list = storeCategoryRepository.findByCode(code);
		if(list.size()>0) return list.iterator().next();
		return null;
	}


	@Override
	public StoreCategory save(StoreCategory cate) {
		return storeCategoryRepository.save(cate);
	}
	
	@Override
	public int updateByPrimaryKey(StoreCategory cate) {
		try {
			String sql = SqlUtils.update(cate, true, " where id = '"+cate.getId()+"'");
			return jdbcTemplate.update(sql);
		} catch ( Exception e) {
			
		}
		return 0;
	}

	@Override
	public StoreCategory findById(String id) {
		return storeCategoryRepository.findOne(id);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		storeCategoryRepository.delete(id);
	}

	@Override
	public int countByCode(String code) {
		return storeCategoryRepository.countByCode(code);
	}

	 

	
}
